package hello.aspect;

import hello.mappers.UserMapper;
import hello.pojos.Users;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.jdbc.core.JdbcOperations;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * Created by Administrator on 2017/8/24.
 */
@RunWith(SpringJUnit4ClassRunner.class)
//@ContextConfiguration(value = "classpath*:applicationContext.xml")
@SpringBootTest
public class AspectJunitTest {

//    @Resource
//    AspectTest aspectTest;
    @Resource
    UserMapper userMapper;
    @Resource
    JdbcOperations jdbcOperations;


    @Test
    public void Test() {
//        aspectTest.out();
//        aspectTest.outMessage("zhangsan", "123123{username}");
    }

    @Test
    @Transactional(propagation = Propagation.REQUIRED)
    public void jta() {
//        Users userIn = new Users(null, "yzhang", "yan", (byte)24);
//        int i = userMapper.insert(userIn);
//        System.out.println("插入的返回记录为:" + i);
//        Users user = userMapper.selectByPrimaryKey(2);
//        System.out.println(user.toString());
        String query = "select * from users where id = 1";
        Users users = (Users)jdbcOperations.queryForObject(query, new RowMapper<Users>() {
            @Override
            public Users mapRow(ResultSet rs, int rowNum) throws SQLException {
                Users user = new Users();
                user.setId(rs.getInt("id"));
                user.setUserName(rs.getString("user_name"));
                user.setAge(rs.getInt("age"));
                return user;
            }
        });
        System.out.println(users.toString());
        String sql = "insert into users(user_name,aliase,age) values('yzhang','yan',24)";
        jdbcOperations.execute(sql);
        String sql1 = "insert into users(id,user_name,aliase,age) values(1,'yzhang','yan',24)";
        jdbcOperations.execute(sql1);
    }
}
